using System;
using System.Collections.Generic;
using BikeSystem.Models;
using System.IO;
using Db4objects.Db4o;
using Db4objects.Db4o.Query;
using Db4objects.Db4o.Config;

namespace BikeSystem.DAL
{
    public class BikeService
    {
        readonly static string YapFileName = Path.Combine(Environment.CurrentDirectory, "bikesystem.yap");

        public Bike GetBikeByBikeNo(string BikeNo)
        {

            using (IObjectContainer db = Db4oEmbedded.OpenFile(YapFileName))
            {

                Bike bike = new Bike();
                IQuery query = db.Query();
                query.Constrain(typeof(Bike));
                query.Descend("bikeNo").Constrain(BikeNo.Trim()).Equal();
                IObjectSet result = query.Execute();
                if (result.HasNext())
                {
                    return (Bike)result.Next();
                }
                return null;
            };

        }

        public IList<Bike> GetAllBike()
        {

            IList<Bike> bikes = new List<Bike>();
            using (IObjectContainer db = Db4oEmbedded.OpenFile(YapFileName))
            {
                IObjectSet result = db.QueryByExample(typeof(Bike));
                while (result.HasNext())
                {
                    bikes.Add((Bike)result.Next());
                }

                return bikes;
            };
        }

        public void InsertBike(Bike bike)
        {
            using (IObjectContainer db = Db4oEmbedded.OpenFile(YapFileName))
            {
                bike.Date = System.DateTime.Now;
                bike.IsNew = false;
                bike.IsUpdated = false;

                db.Store(bike);
                Console.WriteLine("Stored {0}", bike);
            }
        }

        public int DeleteBike(string BikeNo)
        {
            IEmbeddedConfiguration config = Db4oEmbedded.NewConfiguration();
            config.Common.ObjectClass(typeof(Bike)).CascadeOnDelete(true);
            using (IObjectContainer db = Db4oEmbedded.OpenFile(config, YapFileName))
            {
                Bike bike = new Bike();
                bike.BikeNo = BikeNo;
                IObjectSet result = db.QueryByExample(bike);
                if (result.HasNext())
                {
                    db.Delete(result.Next());
                    return 0;
                }
                else
                {
                    return -1;
                }

            }

        }


        public int UpdateBike(Bike updatedBike)
        {
            IEmbeddedConfiguration config = Db4oEmbedded.NewConfiguration();
            config.Common.ObjectClass(typeof(Bike)).CascadeOnUpdate(true);
            using (IObjectContainer db = Db4oEmbedded.OpenFile(config, YapFileName))
            {
                Bike qbike = new Bike();
                qbike.BikeNo = updatedBike.BikeNo;

                IObjectSet result = db.QueryByExample(qbike);
                if (result.HasNext())
                {

                    Bike bike = (Bike)result.Next();

                    bike.DailyRate = updatedBike.DailyRate;
                    bike.HourlyRate = updatedBike.HourlyRate;
                    bike.ModelType = updatedBike.ModelType;


                    bike.Status = updatedBike.Status;
                    bike.Remark = updatedBike.Remark;
                    db.Store(bike);
                    return 0;
                }
                else
                {
                    return -1;
                }
            }

        }



    }
}
